﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Flexible.Implementions.Bases;
using Flexible.Interfaces;

namespace Flexible.Wpf
{
    public class PluginManagerPlugin : PluginBase
    {
        MenuItem tool;
        MenuItem manager;

        public override void Enable(IPluginActivateContext context)
        {
            MenuPlugin menuPlugin = context.GetRequirement("Flexible.Wpf.Menu") as MenuPlugin;
            Menu menu = menuPlugin.Menu;
            GetToolMenuItem(menu);
            if (tool == null)
            {
                tool = new MenuItem();
                tool.Header = "_Tools";
                menu.Items.Add(tool);
            }

            manager = new MenuItem();
            manager.Header = "Plugin _Manager";
            manager.Click += (s, o) => new ManagerWindow().ShowDialog();
            tool.Items.Add(manager);

        }

        private void GetToolMenuItem(Menu menu)
        {
            tool = null;
            foreach (object o in menu.Items)
            {
                MenuItem item = o as MenuItem;
                if (item != null)
                {
                    string header = item.Header as string;
                    if (header == "_Tools")
                    {
                        tool = item;
                        break;
                    }
                }
            }
        }

        public override void Disable(IPluginActivateContext context)
        {
            MenuPlugin menuPlugin = context.GetRequirement("Flexible.Wpf.Menu") as MenuPlugin;
            Menu menu = menuPlugin.Menu;

            GetToolMenuItem(menu);
            if (tool != null)
            {
                tool.Items.Remove(manager);

                if (tool.Items.Count == 0)
                {
                    menu.Items.Remove(tool);
                }
            }
        }

        public override IEnumerable<IPluginQuery> Requirements
        {
            get
            {
                yield return new PluginQuery("Flexible.Wpf.Menu", new Version(1, 0, 0, 0));
            }
        }

        #region Info
        public override string Name
        {
            get { return "Plugin Manager"; }
        }

        public override string Description
        {
            get { return "Dinymically enables/disables plugins."; }
        }

        public override string Author
        {
            get { return "deerchao"; }
        }

        public override DateTime ReleaseTime
        {
            get { return new DateTime(2008, 4, 10); }
        }

        public override string Id
        {
            get { return "Flexible.Wpf.PluginManager"; }
        }

        public override Version Version
        {
            get { return new Version(1, 0, 0, 0); }
        }

        public override Version CompatibleVersion
        {
            get { return new Version(1, 0, 0, 0); }
        }
        #endregion
    }

    public class ManagerWindow : Window
    {
        ListView pluginList;

        public ManagerWindow()
        {
            Title = "Plugin Manager";

            pluginList = new ListView();
            Content = pluginList;
        }

    }
}
